package com.droid.lib.controls.demo.view;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.droid.lib.controls.demo.R;
import com.smarttablayout.SmartTabLayout;
import com.smarttablayout.demo.NoScrollViewPager;
import com.smarttablayout.utils.v4.FragmentPagerItem;
import com.smarttablayout.utils.v4.FragmentPagerItemAdapter;
import com.smarttablayout.utils.v4.FragmentPagerItems;

/**
 * 所有图表
 */
public class AllChartFragment extends Fragment {
    private AllChartsFragmentEntity mFragmentEntity;
    private SmartTabLayout mstlTab;
    private FragmentPagerItemAdapter mtabAdapter;
    private FragmentPagerItems mPages;
    private NoScrollViewPager mnsvpContent;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_all_chart, null);
        ViewGroup tab = (ViewGroup) view.findViewById(R.id.tab);
        mFragmentEntity = AllChartsFragmentEntity.UNFINISHED;
        tab.addView(LayoutInflater.from(getContext()).inflate(mFragmentEntity.layoutResId, tab, false));

        mstlTab = (SmartTabLayout) view.findViewById(R.id.viewpagertab);
        mnsvpContent = (NoScrollViewPager) view.findViewById(R.id.nsvp_content);
        int i = 0;
        mPages = new FragmentPagerItems(getContext());
        for (String titleResId : mFragmentEntity.tabs()) {
            FragmentPagerItem item = FragmentPagerItem.of(titleResId, mFragmentEntity.getClazz(i++));
            mPages.add(item);
        }
        mtabAdapter = new FragmentPagerItemAdapter(getActivity().getSupportFragmentManager(), mPages);
        mnsvpContent.setAdapter(mtabAdapter);
        mstlTab.setViewPager(mnsvpContent);
        mnsvpContent.addOnPageChangeListener(new MyOnPageChangeListener());
        if (mstlTab.getTabCount() > 0) {
            mstlTab.getTabAt(0).setTag(0);
            mPages.get(0).setTag(0);
        }
        mnsvpContent.setMintCurrentIndex(10);
        mnsvpContent.setMintMaxIndex(11);
        return view;
    }

    /**
     * 页卡切换监听
     *
     * @author weizhi
     * @version 1.0
     */
    class MyOnPageChangeListener implements ViewPager.OnPageChangeListener {

        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {

        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    }
}
